﻿using System;
using System.Text;
using System.Data;
using System.Collections;
using DataBaseConnection;

namespace DataEntity
{
    /// <summary>
    /// 管理员
    /// </summary>
    public partial class tab_Admin : IEntity
    {
        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="ID">用户编号</param>
        /// <param name="newpass">新密码</param>
        public static void UpdatePass(int ID, string newpass)
        {
            tab_Admin t = new tab_Admin(true);
            t.adm_LoginPass = newpass;
            t.Update(ID);
        }

        /// <summary>
        /// 检查用户名称存在
        /// </summary>
        /// <param name="RegUserName">新注册用户</param>
        /// <returns></returns>
        public static bool CheckUserName(string RegUserName)
        {
            return DataEntity.Common.ExecuteScalar<bool>(
                "SELECT COUNT(*) FROM tab_Admin WHERE adm_LoginUser = @adm_LoginUser",
                new string[] { "@adm_LoginUser" },
                new object[] { RegUserName }
                );
        }

        /// <summary>
        /// 登录后台
        /// </summary>
        /// <param name="UserName">用户名</param>
        /// <param name="PassWord">密码</param>
        /// <returns>错误及没有，返回-1</returns>
        public void Login(string UserName, string PassWord)
        {
            Select("adm_LoginUser = @adm_LoginUser and adm_LoginPass = @adm_LoginPass",
                new string[] { "@adm_LoginUser", "@adm_LoginPass" },
                new object[] { UserName, PassWord }
                );
        }

        /// <summary>
        /// 获取用户提问
        /// </summary>
        /// <param name="UserName">用户名称</param>
        /// <returns></returns>
        public static string GetQuestion(string UserName)
        {
            tab_Admin t = new tab_Admin();
            t.Select("adm_LoginUser = @adm_LoginUser", new string[] { "@adm_LoginUser" }, new object[] { UserName });
            return t.adm_Question;
        }

        /// <summary>
        /// 创建新密码
        /// </summary>
        /// <param name="UserName">用户帐号</param>
        /// <param name="Answer">回答</param>
        /// <param name="NewRandomPass">新密码</param>
        /// <returns>是否成功</returns>
        public static bool CreateNewPass(string UserName, string Answer, string NewRandomPass)
        {
            tab_Admin t = new tab_Admin(true);
            t.adm_LoginPass = NewRandomPass;
            int rs = t.Update("adm_LoginUser = @adm_LoginUser and adm_Answer = @adm_Answer",
                new string[] { "@adm_LoginUser", "@adm_Answer" },
                new object[] { UserName, Answer }
                );
            return rs != 0;
        }
    }
}

